package org.budo.sharding.util;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import io.shardingsphere.api.config.rule.MasterSlaveRuleConfiguration;
import io.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;

/**
 * @author lmw
 */
public class ShardingUtil {
    public static DataSource createMasterSlaveDataSource(Map<String, DataSource> dataSourceMap, MasterSlaveRuleConfiguration masterSlaveRuleConfig) {
        try {
            HashMap<String, Object> configMap = new HashMap<String, Object>();
            Properties properties = new Properties();

            return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, //
                    masterSlaveRuleConfig, //
                    configMap, //
                    properties);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static DataSource createApacheShardingMasterSlaveDataSource(Map<String, DataSource> dataSourceMap, org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration masterSlaveRuleConfig) {
        try {
            Properties properties = new Properties();

            return org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, //
                    masterSlaveRuleConfig, //
                    properties);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}